package com.video.internationalvideodisplayproject.controller;

import com.video.internationalvideodisplayproject.pojo.dArcStaff;
import com.video.internationalvideodisplayproject.pojo.sysLoginUser;
import com.video.internationalvideodisplayproject.service.dArcStaffService;
import com.video.internationalvideodisplayproject.service.sysLoginUserService;
import com.video.internationalvideodisplayproject.utils.AjaxResult;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.regex.Pattern;

import static com.video.internationalvideodisplayproject.utils.getMD5Password.customizedMD5;


@RestController
@RequestMapping("/staff")
public class dArcStaffController {


    @Autowired
    private dArcStaffService dArcStaffService;
    @Autowired
    private sysLoginUserService sysLoginUserService;

    // C开头用户名的正则表达式
    private static final Pattern C_PREFIX_PATTERN = Pattern.compile("^[Cc]\\w+");


    /**
     * 根据是客户编号以及密码获取到当前对象是否存在
     *
     * @param username
     * @param password
     * @return
     */
    @GetMapping("/getStaffInfoByIdAndPwd/{username}/{password}")
    public AjaxResult getStaffInfoByIdAndPassword(@PathVariable String username, @PathVariable String password) {
        if (StringUtils.isEmpty(username) || StringUtils.isBlank(password)) {
            return AjaxResult.error("The parameters are not defined");
        }


        // 将用户名称和密码都去除空格
        username = username.trim();
        password = password.trim();

        // 判断
        if (C_PREFIX_PATTERN.matcher(username).matches()) {
            dArcStaff dArcStaff = dArcStaffService.selectStaffInfoByIdAndPassword(username, password);

            if (ObjectUtils.isEmpty(dArcStaff)) {
                return AjaxResult.error("Invalid username or password.");
            }

            if (username.equals(dArcStaff.getStaffId()) && password.equals(dArcStaff.getNew_password())) {
                return AjaxResult.success(dArcStaff);
            } else {
                return AjaxResult.error("Please check your username and password.");
            }
        } else {
            // 这个用户名称是依MS开头
            sysLoginUser sysLoginUser = null;
            if (StringUtils.isNotBlank(username)) {
                username = username.toUpperCase();
                // 判断当前对象是否存在
                sysLoginUser = sysLoginUserService.selectLoginInfoByUserId(username);
            }

            if (ObjectUtils.isEmpty(sysLoginUser)) {
                return AjaxResult.error("The current account does not exist, please check if your account exists!");
            }

            // 账号存在继续执行
            // 如果当前账号存在，那么判断当前密码是否一致
            // 使用md5加密与原数据进行比对
            String md5Password = null;
            if (ObjectUtils.isNotEmpty(sysLoginUser.getUserPassword())) {
                md5Password = customizedMD5(password);
            }

            if (StringUtils.isNotBlank(username)) {
                // 判断账号是否一样
                if (!username.equals(sysLoginUser.getUserId())) {
                    return AjaxResult.error("当前输入的【" + username + "】不存在!");
                }
            }

            // 判断密码
            if (!sysLoginUser.getUserPassword().equals(md5Password)) {
                return AjaxResult.error("The password you are currently entering is incorrect, please check the password carefully!");
            } else {
                return AjaxResult.success("Login is successful, welcome to the video page");
            }

        }


    }
}
